/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatec.gilberto;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author gilberto
 */
public class BaseXML {

    public BaseXML() {
    }
    private Vector categorias = new Vector();
    private Vector alimentos = new Vector();
    private String currentCategoria;
    private Alimento arr[] = new Alimento[10000];

    public String getBaseXML() {
        InputStream input = getClass().getResourceAsStream("base/dbcontrolecal.xml");
        //Abre fluxos de saída para escrever o conteúdo do arquivo
        ByteArrayOutputStream output = new ByteArrayOutputStream(1024);

        byte[] buffer = new byte[512];

        int bytes;
        try {
            while ((bytes = input.read(buffer)) > 0) {
                output.write(buffer, 0, bytes);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            input.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }


        return output.toString();
    }

    public Vector getCategorias() throws IOException {
        categorias.removeAllElements();
        try {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            InputStream is = new ByteArrayInputStream(getBaseXML().getBytes());
            parser.parse(is, new DefaultHandler() {
                public void startElement(String uri, String lname, String name, Attributes att) throws SAXException {
                    if (name.equals("categoria")) {
                        categorias.addElement(att.getValue(0));
                    }
                }
            });
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }

        return categorias;
    }

    public Alimento[] getAlimentos(String categoria) throws IOException {
        alimentos.removeAllElements();
        currentCategoria = categoria;
        try {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            InputStream is = new ByteArrayInputStream(getBaseXML().getBytes());
            parser.parse(is, new DefaultHandler() {
                String currentName;
                Attributes currentAtributo = null;
                String cat;
                int cont = 0;

                public void startElement(String uri, String lname, String name, Attributes att) throws SAXException {
                    currentName = name;
                    if (name.equals("categoria")) {
                        currentAtributo = att;
                        cat = currentName;
                    }
                }

                public void characters(char[] ch, int start, int length) {
                    String chars = new String(ch, start, length);
                    if (currentName.equals("categoria")) {
                        cat = currentName;
                    }

                    if (currentAtributo != null) {

                        if (currentAtributo.getValue(0).equals(currentCategoria)) {
                            Alimento almt = new Alimento();
                            if (currentName.equals("nome")) {
                                System.out.println(chars.trim());
                                almt.setNome(chars.trim());
                            }
                            if (currentName.equals("medida")) {
                                System.out.println(chars.trim());
                                almt.setMedida(chars.trim());
                            }
                            if (currentName.equals("peso")) {
                                if (!chars.trim().equals("")) {
                                    System.out.println("|" + chars.trim() + "|");
                                    almt.setPesoEmGramas(chars.trim());
                                }
                            }
                            if (currentName.equals("calorias")) {
                                System.out.println(chars.trim());
                                almt.setCalorias(Integer.parseInt(chars.trim().toString()));
                            }

                            //alimentos.addElement("chunbrega");
                            if (almt.getNome() != null) {
                                arr[cont] = almt;
                                cont++;
                            }
                        }

                    }
                }
            });
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }

        return arr;
    }
}
